//
//  AddTypeViewController.m
//  LazyCat
//
//  Created by jike on 15/12/22.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "AddTypeViewController.h"
#import "AFNetworkHandler.h"
#import "fatherClassifyViewController.h"

@interface AddTypeViewController ()<UITextFieldDelegate,backFuStrDelegate>

@property (nonatomic,strong) NSString *one;//第一排名字
@property (nonatomic,strong) NSString *two;//第二排排序值
@property (nonatomic,strong) NSString *three;//第三排父类
@property (nonatomic,strong) NSString *four;//第四排是否启用
@property (nonatomic,strong) UITextField *textFile3;//父类名字
@property (nonatomic,strong) NSString *five;//用于传父类id

@end

@implementation AddTypeViewController

-(void)backFuStr:(NSString *)str identify:(NSString *)idf {
    self.textFile3.text = [NSString stringWithFormat:@"%@",str];
    self.three = str;
    self.five = idf;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav:@"编辑分类" doBack:YES];
    UIView *lineV= [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)] ;
    lineV.backgroundColor = [UIColor lightGrayColor];
    lineV.alpha = 0.2;
    [self.navBar addSubview:lineV];
    self.view.backgroundColor = BackColor;
    [self cretView];//创建视图
    if ([self.typeLevel isEqualToString:@"0"]) {
        self.one = [self.dict objectForKey:@"categoryname"];
        self.two = [self.dict objectForKey:@"sort"];
        self.three = [self.dict objectForKey:@"categoryname"];
        self.four = [self.dict objectForKey:@"isactived"];
        self.five = @"";
    }else {
        NSArray *arr = self.dict[@"childrencategory"];
        NSInteger row = [self.rowNum integerValue];
        self.one = [NSString stringWithFormat:@"%@",[arr[row] objectForKey:@"categoryname"]];
        self.two = [NSString stringWithFormat:@"%@",[arr[row] objectForKey:@"sort"]];
        self.three = [self.dict objectForKey:@"categoryname"];
        self.four = [NSString stringWithFormat:@"%@",[arr[row] objectForKey:@"isactived"]];
        self.five = self.dict[@"categoryid"];
    }
}
-(void)cretView {//创建视图
    //第一行
    UILabel *lable1 = [[UILabel alloc] initWithFrame:CGRectMake(0,65 , ScreenWidth, 50)];
    lable1.backgroundColor = [UIColor whiteColor];
    lable1.text = @"  分类名称";
    lable1.font = [UIFont fontWithName:nil size:14];
    [self.view addSubview:lable1];
    UITextField *textFile1 = [[UITextField alloc] initWithFrame:CGRectMake(80, 65, ScreenWidth - 80, 50)];
    textFile1.tag = 100;
    textFile1.clearButtonMode = UITextFieldViewModeWhileEditing;
    textFile1.delegate = self;
    textFile1.font = [UIFont fontWithName:nil size:14];
    textFile1.placeholder = @"请填写分类名称";
    [self.view addSubview:textFile1];
    //第二行
    UILabel *lable2 = [[UILabel alloc] initWithFrame:CGRectMake(0,65 + 51 , ScreenWidth, 50)];
    lable2.backgroundColor = [UIColor whiteColor];
    lable2.text = @"  排序值";
    lable2.font = [UIFont fontWithName:nil size:14];
    [self.view addSubview:lable2];
    UITextField *textFile2 = [[UITextField alloc] initWithFrame:CGRectMake(80, 65 + 51, ScreenWidth - 80, 50)];
    textFile2.tag = 200;
    textFile2.delegate = self;
    textFile2.keyboardType = UIKeyboardTypeNumberPad;
    textFile2.clearButtonMode = UITextFieldViewModeWhileEditing;
    textFile2.font = [UIFont fontWithName:nil size:14];
    textFile2.placeholder = @"排序值越大越靠前";
    [self.view addSubview:textFile2];
    //第三行
    UILabel *lable3 = [[UILabel alloc] initWithFrame:CGRectMake(0,65 + 51+51 , ScreenWidth, 50)];
    lable3.backgroundColor = [UIColor whiteColor];
    lable3.text = @"  父级分类";
    lable3.font = [UIFont fontWithName:nil size:14];
    [self.view addSubview:lable3];
    self.textFile3 = [[UITextField alloc] initWithFrame:CGRectMake(80, 65 + 51 + 51, ScreenWidth - 110, 50)];
    self.textFile3.tag  = 300;
    self.textFile3.delegate = self;
    [self.textFile3 setEnabled:NO];//不可编辑
    self.textFile3.font = [UIFont fontWithName:nil size:14];
    self.textFile3.placeholder = @"添加一级分类请留空";
    [self.view addSubview:self.textFile3];
    lable3.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jump:)];//跳到所有父类
    [lable3 addGestureRecognizer:tap];
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-23, 65 + 51 + 51 +17, 10, 15)];
    imageV.alpha = 0.5;
    imageV.image = [UIImage imageNamed:@"huijiantou"];
    [self.view addSubview:imageV];
    UILabel *lable4 = [[UILabel alloc] initWithFrame:CGRectMake(0,65 + 51+51+51 , ScreenWidth, 50)];
    //第四行
    lable4.backgroundColor = [UIColor whiteColor];
    lable4.text = @"  启用当前分类";
    lable4.font = [UIFont fontWithName:nil size:14];
    [self.view addSubview:lable4];
    UISwitch *swith = [[UISwitch alloc] initWithFrame:CGRectMake(ScreenWidth - 60, 65 + 51+51+61, 60, 30)];
    swith.onTintColor = SetColor(255, 198, 0, 1);
    [swith addTarget:self action:@selector(swithAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:swith];
    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(10,  65+51+51+126, ScreenWidth - 20, 45)];
    addButton.layer.cornerRadius = 5;
    [addButton setTitle:@"保存" forState:UIControlStateNormal];
    [addButton setBackgroundColor:SetColor(255, 198, 0, 1)];
    [addButton addTarget:self action:@selector(addtypeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addButton];
    if ([self.typeLevel isEqualToString:@"0"]) {
        lable3.hidden = YES;
        self.textFile3.hidden = YES;
        imageV.hidden = YES;
        lable4.frame = CGRectMake(0,65+51+51 , ScreenWidth, 50);
        swith.frame = CGRectMake(ScreenWidth-60, 65+51+51+10, 60, 30);
        addButton.frame = CGRectMake(10,  65+51+126, ScreenWidth-20, 45);
        textFile1.text = [self.dict objectForKey:@"categoryname"];
        textFile2.text = [self.dict objectForKey:@"sort"];
        NSString *isAnvance = [self.dict objectForKey:@"isactived"];
        if ([isAnvance isEqualToString:@"0"]) {
            swith.on = NO;
        }else {
            swith.on = YES;
        }
    }else {
        self.textFile3.text = [self.dict objectForKey:@"categoryname"];
        NSArray *arr = self.dict[@"childrencategory"];
        NSInteger row = [self.rowNum integerValue];
        textFile1.text = [NSString stringWithFormat:@"%@",[arr[row] objectForKey:@"categoryname"]];
        textFile2.text = [NSString stringWithFormat:@"%@",[arr[row] objectForKey:@"sort"]];
        NSString *isAdvance = [NSString stringWithFormat:@"%@",[arr[row] objectForKey:@"isactived"]];
        if ([isAdvance isEqualToString:@"0"]) {
            swith.on = NO;
        }else {
            swith.on = YES;
        }
    }
}
-(void)jump:(UITapGestureRecognizer *)tap {
    fatherClassifyViewController *fathVC = [[fatherClassifyViewController alloc]init];
    fathVC.faArr = self.fuArr;
    fathVC.faStr = self.three;
    fathVC.delegate = self;
    [self.navigationController pushViewController:fathVC animated:YES];
}
#pragma mark - UITextField
-(void)textFieldDidEndEditing:(UITextField *)textField {
    [textField resignFirstResponder];
    if (textField.tag == 100) {
        self.one = textField.text;
    }else if (textField.tag == 200){
        self.two = textField.text;
    }else if (textField.tag == 300){
        self.three = textField.text;
    }
}
- (BOOL)textFieldShouldClear:(UITextField *)textField {
    return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
-(void)addtypeAction {// 添加按钮
    [self.view endEditing:YES];
    if (self.one.length == 0) {
        [[MessShowView share] showWithMess:@"请输入分类名称" login:NO];
        return;
    }
    if ([self.typeLevel isEqualToString:@"1"]) {
        NSArray *arr = self.dict[@"childrencategory"];
        NSInteger row = [self.rowNum integerValue];
        NSString *sonID = [arr[row] objectForKey:@"categoryid"];
        NSString *url = [NSString stringWithFormat:editClassifyUrl,sonID,self.one,self.two,self.five,self.four];
        [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
            NSDictionary *dic = result;
            NSString *r = dic[@"r"];
            if ([r isEqualToString:@"T"]) {
                [self.delegate passValue:@"yesm"];
                [[MessShowView share] showWithMess:@"保存成功" login:YES];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [[MessShowView share] showWithMess:@"保存失败" login:NO];
            }
        }];
    }else {
        NSString *sonID = self.dict[@"categoryid"];
        NSString *url = [NSString stringWithFormat:editClassifyUrl,sonID,self.one,self.two,@"",self.four];
        [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
            NSDictionary *dic = result;
            NSString *r = dic[@"r"];
            if ([r isEqualToString:@"T"]) {
                [self.delegate passValue:@"yesn"];
                [[MessShowView share] showWithMess:@"保存成功" login:YES];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [[MessShowView share] showWithMess:@"保存失败" login:NO];
            }
        }];
    }
    
}
-(void)swithAction:(UISwitch *)swith {//开关按钮
    if (swith.on) {
        self.four = @"1";
    }else{
        self.four = @"0";
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
